package sincon.laogao.deepseek.controller;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/ai")
public class easyAiController {
    private static final String API_KEY = "sk-2d525ebc32ba41cd8e57064332480226";
    private static final String BASE_URL = "https://api.deepseek.com";
    private static final OkHttpClient client =  new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .build();


    @PostMapping("/msg")
    public String createEvent(@RequestBody String req) {
        String response = "";
        try {
               response = createChatCompletion(false,req);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return response;
    }
    private static String createChatCompletion(boolean streamFlag, String next) throws Exception {

        String url = BASE_URL + "/v1/chat/completions";

        JSONArray messages = new JSONArray()
                .put(new JSONObject().put("role", "system").put("content", ",你是一名我的世界资深技术,你参与过mojang我的世界开发," +
                        "也自己搭建过我的世界java服务器,同时熟悉所有MineCraft的插件,并能准确的识别报错和提供指导,希望你准确无误的回答我的问题," +
                        "并且不能回复超过500字！"))
//                .put(new JSONObject().put("role", "user").put("content", "Hello! How can I assist you today? \uD83D\uDE0A"));
                .put(new JSONObject().put("role", "user").put("content", next));

        JSONObject jsonBody = new JSONObject()
                .put("model", "deepseek-chat")
                .put("messages", messages)
                .put("stream", streamFlag);

        okhttp3.RequestBody body = okhttp3.RequestBody.create(
                jsonBody.toString(),
                MediaType.get("application/json; charset=utf-8")
        );

        Request request = new Request.Builder()
                .url(url)
                .header("Authorization", "Bearer " + API_KEY)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        if(!streamFlag) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            ResponseBody responseBody = response.body();
            if (responseBody != null) {
                JSONObject jsonResponse = new JSONObject(responseBody.string());
                return jsonResponse.getJSONArray("choices")
                        .getJSONObject(0)
                        .getJSONObject("message")
                        .getString("content");
            }
        }else{

        }

        return null;
    }

}
